package com.goods.business.service.imp;


import com.goods.business.mapper.BizProductCategoryMapper;
import com.goods.business.service.BizProductCategoryService;
import com.goods.common.error.BaseError;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.BizProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BizProductCategoryServiceImpl implements BizProductCategoryService  {
    @Autowired
    BizProductCategoryMapper bizProductCategoryMapper;
    @Override
    public List<ProductCategoryTreeNodeVO> selectPage(int pageNum, int pageSize){
        List<BizProductCategory> bizProductCategories = bizProductCategoryMapper.selectAll();
        ArrayList<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
        for (BizProductCategory bizProductCategory : bizProductCategories) {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO= new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(bizProductCategory,productCategoryTreeNodeVO);
            productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO);
        }
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(build, pageSize,pageNum);
        return page;

    }

    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<BizProductCategory> bizProductCategories = bizProductCategoryMapper.selectAll();
        ArrayList<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
        for (BizProductCategory bizProductCategory : bizProductCategories) {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO= new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(bizProductCategory,productCategoryTreeNodeVO);
            productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO);
        }
        List<ProductCategoryTreeNodeVO> parentCategoryTree = CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);
        return parentCategoryTree;
    }

    @Override
    public void add(BizProductCategory bizProductCategory) {
        bizProductCategory.setCreateTime(new Date());
        bizProductCategory.setModifiedTime(new Date());
        bizProductCategoryMapper.insert(bizProductCategory);
    }

    @Override
    public BizProductCategory findById(long id) {
        BizProductCategory bizProductCategory = new BizProductCategory();
        bizProductCategory.setId(id);
        bizProductCategory = bizProductCategoryMapper.selectOne(bizProductCategory);
        return bizProductCategory;
    }

    @Override
    public void update(BizProductCategory bizProductCategory) {
        bizProductCategory.setModifiedTime(new Date());
        bizProductCategoryMapper.updateByPrimaryKeySelective(bizProductCategory);
    }

    @Override
    public void deleteById(long id) throws Exception {
        BizProductCategory bizProductCategory = new BizProductCategory();
        bizProductCategory.setPid(id);
        int count = bizProductCategoryMapper.selectCount(bizProductCategory);
        if (count>0) {
            throw new BusinessException(BusinessCodeEnum.INTERNAL_SERVER_ERROR,"有子级分类，不能删除");
        }
        bizProductCategory.setPid(null);
        bizProductCategory.setId(id);
        bizProductCategoryMapper.delete(bizProductCategory);
    }
}
